#include <cmath>
#include <iostream>
#include <fstream>

#include "include/poisson_class.h"

/*Poisson::Poisson(double L,double z0, double x0) {

}*/

Poisson::~Poisson() {

}

double Poisson::Funct(double x) {
  double beta = 2;
  double alfa = 0.375;
  return beta*exp(-alfa*x);
}

void Poisson::Calc() {

  h_ = L_/N_;
  psi_.push_back(12.56);

  delta_.push_back(h_*(z0_+1/2*h_*Funct(x0_)));
  psi_.push_back(psi_[0]+delta_[0]);

  for(int k=1; k<=N_-1; k++) {
    delta_.push_back(delta_[k-1]+h_*h_*Funct(x0_+ k*h_));
    psi_.push_back(psi_[k]+delta_[k]);
}
  zn_=delta_[N_-1]/h_ + 1/2*h_*Funct(x0_+L_);
  Write_file(psi_, h_);
}

void Poisson::Write_file(const std::vector <double> &psi, double h) {
  std::ofstream out("result.txt", std::ofstream::binary);
  if(!out) {
    std::cout<<"Unable to open file\n";
   // return 1;
  }
  double temp = 0;
  for(unsigned int i=0; i <= psi.size()-1; i++) {
    temp+=h;
    out<<temp<<' ';
    out<<psi[i]<<'\n';
  }
  out.close();
}
